package edu.utd.text.game.tests;

import java.util.ArrayList;

import junit.framework.Assert;
import junit.framework.TestCase;
import edu.utd.text.game.domain.GameMap;
import edu.utd.text.game.domain.areas.Room;
import edu.utd.text.game.domain.exceptions.DuplicateRoomException;

public class TestGameMap extends TestCase {

	public void testAddRoom() {
		Room room1 = new Room("room1");
		Room room2 = new Room("room2");
		GameMap map = new GameMap();
		boolean caughtEx = false;
		try {
			map.addRoom(room1);
			map.addRoom(room1);
		} catch (DuplicateRoomException drx) {
			caughtEx = true; } 			//should get here	
		Assert.assertTrue(caughtEx);
		
		try {
			ArrayList<Room> hSet = new ArrayList<Room>();
			hSet.add(room1);
			map = new GameMap(hSet);
			map.addRoom(room2);
		} catch (DuplicateRoomException drx) {
			fail("UNEXPECTED exception!");
			drx.printStackTrace(); }
	}
	
	public void testContainsRoom() {
		GameMap map = new GameMap();
		try {
			map.addRoom(new Room("room1"));
		} catch (DuplicateRoomException drx) { 
			fail("UNEXPECTED exception while adding room to game map.");
		}
		if (!map.containsRoom(new Room("room1"))) {
			fail("2 diff instances with same namewere treated as different - WRONG!");
		}
		boolean containsRoom = map.containsRoom(new Room("room2)"));
		if(containsRoom) {
			fail("Found a match in the GameMap even though 2 different rooms.");
		}
		
		Room r1 = new Room("R1");
		try { map.addRoom(r1); } 
		catch (DuplicateRoomException drx) { 
			fail("UNEXPECTED exception while adding room to game map."); }
		Assert.assertTrue(map.containsRoom(r1));
	}
	
	public void testContainsRoomsByName() {
		GameMap map = new GameMap();
		try {
			map.addRoom(new Room("room1"));
		} catch (DuplicateRoomException drx) { 
			fail("UNEXPECTED exception while adding room to game map.");
		}
		if (!map.containsRoomByName((new Room("room1")).getRoomName())) {
			fail("2 diff instances shuold have been treated as same in this case");
		}
	}
	
	public void testRemoveRoom() {
		ArrayList<Room> hSet = new ArrayList<Room>();
		Room room1 = new Room("room1");
		Room room2 = new Room("room2");
		hSet.add(room1);
		hSet.add(room2);
		GameMap map = new GameMap(hSet);
		//remove a room that exists in the game map
		boolean removed = map.removeRoom(room1);
		//did it really get removed?
		boolean containsRoom = map.containsRoom(room1);
		if(!removed) {
			fail("GameMap.remove() did not return TRUE when expected");
		}
		if(containsRoom) {
			fail("We didn't really remove the room we said we did from the GameMap");
		}
		//remove a room that doesn't exist in the game map
		removed = map.removeRoom(room1);
		if(removed) {
			fail("WHAT???  We should get a FALSE return from removeRoom() here...");
		}
	}
	
}
